package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * account表 添加一条记录 insert 语句
 */
public class Demo02 {
    public static void main(String[] args) {
//        1. 导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
//        1.复制mysql-connector-java-5.1.37-bin.jar到项目的libs目录下
//        2.右键-->Add As Library

        Connection con = null;
        Statement sta = null;
        try {
            //2. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //3. 获取数据库连接对象 Connection
            con = DriverManager.getConnection("jdbc:mysql:///db2","root","myroot");
//        4. 定义sql
            String sql = "insert into account values(null,'李四',1000)";
//        5. 获取执行sql语句的对象 Statement
            sta = con.createStatement();
//        6. 运输sql，接受返回结果
            int i = sta.executeUpdate(sql);
//        7. 处理结果
            System.out.println(i);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //        8. 释放资源
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                sta.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
